/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bsd5ex.group1.kaokang;

import com.bsd5ex.group1.kaokang.config.FoodPriceTypeConstants;
import com.bsd5ex.group1.kaokang.dao.FoodPriceTypeDAO;
import com.bsd5ex.group1.kaokang.dao.OrderDAO;
import com.bsd5ex.group1.kaokang.dao.OrderLineItemDAO;
import com.bsd5ex.group1.kaokang.model.Food;
import com.bsd5ex.group1.kaokang.model.FoodPriceType;
import com.bsd5ex.group1.kaokang.model.Order;
import com.bsd5ex.group1.kaokang.model.OrderLineDetail;
import com.bsd5ex.group1.kaokang.model.OrderLineItem;
import java.util.Set;

/**
 *
 * @author IBIAINIKI
 */
public class CalculateOrder {

    public int getOrderTotalPrice(int orderID) {
        return new OrderDAO().getOrderByID(orderID).getTotalPrice();
    }

    public void calculateOrder(int orderID) {
        calculateOrder(new OrderDAO().getOrderByID(orderID));
    }

    public void calculateOrderLineItem(int orderLineItemID) {
        calculateOrderLineItem(new OrderLineItemDAO().getOrderLineItemByID(orderLineItemID));
    }

    public int calculateOrder(Order order) {
        int totalPrice = 0;
        if (order == null) {
            return 0;
        } else {
            Set<OrderLineItem> orderLineItemSet = order.getOrderLineItems();
            OrderLineItem[] orderLineItems = (OrderLineItem[]) orderLineItemSet.toArray();
            int lineItemPrice;
            for (OrderLineItem orderLineItem : orderLineItems) {
                if (orderLineItem == null) {
                    continue;
                }
                lineItemPrice = orderLineItem.getPrice();
                if (lineItemPrice == 0) {
                    lineItemPrice = calculateOrderLineItem(orderLineItem);
                }
                totalPrice += orderLineItem.getPrice();
            }
        }
        return totalPrice;
    }

    public int calculateOrderLineItem(OrderLineItem orderLineItem) {
        if (orderLineItem == null) {
            return 0;
        }
        OrderLineDetail[] orderLineDetails = (OrderLineDetail[]) orderLineItem.getOrderLineDetails().toArray();
        int orderLineItemPrice = 0;
        switch (orderLineDetails.length) {
            case 0: {
                return 0;
            }
            case 1: {
                OrderLineDetail orderLineDetail = null;
                orderLineDetail = orderLineDetails[0];
                Food food = orderLineDetail.getFood();
                FoodPriceTypeDAO foodPriceTypeDAO = new FoodPriceTypeDAO();
                FoodPriceType foodPriceType = foodPriceTypeDAO.getFoodPriceTypeByFoodIdAndTitle(food.getIdFood(), FoodPriceTypeConstants.TYPE_SINGLE);
                orderLineItemPrice = foodPriceType.getFoodPriceGroup().getFoodPrice().getSinglePrice();
                break;
            }
            case 2: {
                for (OrderLineDetail orderLineDetail : orderLineDetails) {
                    Food food = orderLineDetail.getFood();
                    FoodPriceTypeDAO foodPriceTypeDAO = new FoodPriceTypeDAO();
                    FoodPriceType foodPriceType = foodPriceTypeDAO.getFoodPriceTypeByFoodIdAndTitle(food.getIdFood(), FoodPriceTypeConstants.TYPE_SINGLE);
                    orderLineItemPrice += foodPriceType.getFoodPriceGroup().getFoodPrice().getSinglePrice();
                }
                break;
            }
            default: {
                OrderLineDetail orderLineDetail = null;
                for (int i = 0; i < orderLineDetails.length; i++) {
                    orderLineDetail = orderLineDetails[i];
                    if (orderLineDetail == null) {
                        continue;
                    }
                    if (i <= 1) {
                        Food food = orderLineDetail.getFood();
                        FoodPriceTypeDAO foodPriceTypeDAO = new FoodPriceTypeDAO();
                        FoodPriceType foodPriceType = foodPriceTypeDAO.getFoodPriceTypeByFoodIdAndTitle(food.getIdFood(), FoodPriceTypeConstants.TYPE_SINGLE);
                        orderLineItemPrice += foodPriceType.getFoodPriceGroup().getFoodPrice().getSinglePrice();
                    } else {
                        Food food = orderLineDetail.getFood();
                        FoodPriceTypeDAO foodPriceTypeDAO = new FoodPriceTypeDAO();
                        FoodPriceType foodPriceType = foodPriceTypeDAO.getFoodPriceTypeByFoodIdAndTitle(food.getIdFood(), FoodPriceTypeConstants.TYPE_MORE);
                        orderLineItemPrice += foodPriceType.getFoodPriceGroup().getFoodPrice().getMorePrice();
                    }
                }
                break;
            }
        }
        return orderLineItemPrice;
    }
    
    public OrderLineItem getOrderLineItemWithCalculatePrice(OrderLineItem orderLineItem){
        if (orderLineItem == null) {
            return orderLineItem;
        }
        OrderLineDetail[] orderLineDetails = (OrderLineDetail[]) orderLineItem.getOrderLineDetails().toArray();
        int orderLineItemPrice = 0;
        switch (orderLineDetails.length) {
            case 0: {
                return orderLineItem;
            }
            case 1: {
                OrderLineDetail orderLineDetail = null;
                orderLineDetail = orderLineDetails[0];
                Food food = orderLineDetail.getFood();
                FoodPriceTypeDAO foodPriceTypeDAO = new FoodPriceTypeDAO();
                FoodPriceType foodPriceType = foodPriceTypeDAO.getFoodPriceTypeByFoodIdAndTitle(food.getIdFood(), FoodPriceTypeConstants.TYPE_SINGLE);
                orderLineItemPrice = foodPriceType.getFoodPriceGroup().getFoodPrice().getSinglePrice();
                break;
            }
            case 2: {
                for (OrderLineDetail orderLineDetail : orderLineDetails) {
                    Food food = orderLineDetail.getFood();
                    FoodPriceTypeDAO foodPriceTypeDAO = new FoodPriceTypeDAO();
                    FoodPriceType foodPriceType = foodPriceTypeDAO.getFoodPriceTypeByFoodIdAndTitle(food.getIdFood(), FoodPriceTypeConstants.TYPE_SINGLE);
                    orderLineItemPrice += foodPriceType.getFoodPriceGroup().getFoodPrice().getSinglePrice();
                }
                break;
            }
            default: {
                OrderLineDetail orderLineDetail = null;
                for (int i = 0; i < orderLineDetails.length; i++) {
                    orderLineDetail = orderLineDetails[i];
                    if (orderLineDetail == null) {
                        continue;
                    }
                    if (i <= 1) {
                        Food food = orderLineDetail.getFood();
                        FoodPriceTypeDAO foodPriceTypeDAO = new FoodPriceTypeDAO();
                        FoodPriceType foodPriceType = foodPriceTypeDAO.getFoodPriceTypeByFoodIdAndTitle(food.getIdFood(), FoodPriceTypeConstants.TYPE_SINGLE);
                        orderLineItemPrice += foodPriceType.getFoodPriceGroup().getFoodPrice().getSinglePrice();
                    } else {
                        Food food = orderLineDetail.getFood();
                        FoodPriceTypeDAO foodPriceTypeDAO = new FoodPriceTypeDAO();
                        FoodPriceType foodPriceType = foodPriceTypeDAO.getFoodPriceTypeByFoodIdAndTitle(food.getIdFood(), FoodPriceTypeConstants.TYPE_MORE);
                        orderLineItemPrice += foodPriceType.getFoodPriceGroup().getFoodPrice().getMorePrice();
                    }
                }
                break;
            }
        }
        orderLineItem.setPrice(orderLineItemPrice);
        return orderLineItem;
    }
    
    public Order getOrderWithCalculatePrice(Order order){
        int totalPrice = 0;
        if (order == null) {
            return order;
        } else {
            Set<OrderLineItem> orderLineItemSet = order.getOrderLineItems();
            OrderLineItem[] orderLineItems = (OrderLineItem[]) orderLineItemSet.toArray();
            int lineItemPrice;
            for (OrderLineItem orderLineItem : orderLineItems) {
                if (orderLineItem == null) {
                    continue;
                }
                lineItemPrice = orderLineItem.getPrice();
                if (lineItemPrice == 0) {
                    lineItemPrice = calculateOrderLineItem(orderLineItem);
                }
                totalPrice += orderLineItem.getPrice();
            }
            order.setTotalPrice(totalPrice);
            return order;
        }
    }
}